package git;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GitManagement {
    private DataBind dataBind = new DataBind();
    private JFrame frame;
    private JPanel panal;
    private JTextField passwordField;
    private JTable table;
    private JButton refresh;
    private JButton delete;
    private JButton batchDelete;
    private JTextArea remark;

    private DefaultTableModel model;
    private GiteeSdk giteeSdk;

    private void createUIComponents() {
        // 创建一个新的表格模型
        model = new DefaultTableModel(
                new Object[][]{},
                new Object[]{"作者","仓库名称","描述"} // 表头
        );
        table = new JTable(model);
        //设置表格为多选模式
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    }

    public GitManagement(){
        giteeSdk = new GiteeSdk(dataBind);
        frame = new JFrame("Git仓库批量删除工具");
        frame.setContentPane(panal);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        // 添加刷新按钮的点击事件
        refresh.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                getData(dataBind);
                if (dataBind.getPassword().isEmpty()) {
                    JOptionPane.showMessageDialog(frame, "请输入授权码", "提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                try {
                    refreshTable();
                }catch (Exception ex) {
                    JOptionPane.showMessageDialog(frame, "授权码错误", "提示", JOptionPane.INFORMATION_MESSAGE);
                }

            }
        });
        // 添加删除按钮的点击事件
        delete.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int[] selectedRows = table.getSelectedRows();
                if (selectedRows.length != 1) {
                    JOptionPane.showMessageDialog(frame, "请选择一个仓库", "提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                String owner = (String) model.getValueAt(selectedRows[0], 0);
                String repo = (String) model.getValueAt(selectedRows[0], 1);
                // 弹出确认框
                int result = JOptionPane.showConfirmDialog(frame, "确定删除仓库["+repo+"] 吗?", "删除仓库", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    // 删除仓库
                    giteeSdk.deleteRepo(owner, repo);
                    // 从表格中删除
                    model.removeRow(table.getSelectedRow());
                    System.out.println("删除仓库["+repo+"]成功");
                }
            }
        });
        batchDelete.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int[] selectedRows = table.getSelectedRows();
                if (selectedRows.length == 0) {
                    JOptionPane.showMessageDialog(frame, "请至少选择一个仓库", "提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                int result = JOptionPane.showConfirmDialog(frame, "确定删除仓库吗?", "删除仓库", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    for (int i = selectedRows.length - 1; i >= 0; i--) {
                        String owner = (String) model.getValueAt(selectedRows[i], 0);
                        String repo = (String) model.getValueAt(selectedRows[i], 1);
                        giteeSdk.deleteRepo(owner, repo);
                        model.removeRow(selectedRows[i]);
                        System.out.println("删除仓库["+repo+"]成功");
                    }
                }
            }
        });
    }

    public static void main(String[] args) {
        new GitManagement();
    }

    public void setData(DataBind data) {
        passwordField.setText(data.getPassword());
    }

    public void getData(DataBind data) {
        data.setPassword(passwordField.getText());
    }

    // 刷新表格
    private void refreshTable() {
        model.setRowCount(0);
        giteeSdk.getRepos().forEach(resp -> {
            model.addRow(new Object[]{resp.getOwner(), resp.getRepo(),resp.getDescription()});
        });
    }
}
